<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Sanitize data on the way into the application
 * @author Anthony Gallon
 * @package  JFX
 */


/**
 * Sanitize data on the way into the application.
 * @author Anthony Gallon
 * @package  JFX
 */
class JFX_Input extends Antz_TagFilter
{
    /**
     * Reference to $_POST
     *
     * @var reference $post
     */
    protected $post;
    /**
     * Reference to $_GET
     *
     * @var reference $get
     */
    protected $get;
    /**
     * Reference to $_FILES
     *
     * @var reference $files
     */
    protected $files;
    /**
     * Default length of input
     *
     * @var int $strlen
     */
    public $strlen;
    /**
     * Which html tags are allowed?
     *
     * @var array $allowTags
     */
    public $allowTags = array();
    /**
     * Which html attributes are allowed?
     *
     * @var array $allowAtts
     */
    public $allowAtts = array();

    /**
     * Set default values
     *
     */
    public function __construct(){
        parent::__construct();
        $this->post =& $_POST;
        $this->get =& $_GET;
        $this->files =& $_FILES;
        $this->strlen = 255;
        // please contact me if I have overlooked your favourite tag or attribute!

        $this->setAllowAtts(array(
			'class',
			'id',
			'src',
			'alt',
			'title',
			'style',
			'align',
			'href'
			)
		);
        $this->setAllowTags(array(
			'h1',
			'h2',
			'h3',
			'h4',
			'h5',
			'h6',
			'div',
			'p',
			'li',
			'ol',
			'ul',
			'b',
			'a',
                'dl',
                'dd',
                'dt',
                'span',
			'strong',
			'em',
			'i',
			'img',
			'blockquote',
			'dt',
			'dl',
			'dd',
			'code',
                'table',
                'thead',
                'tbody',
                'tr',
                'th',
                'td'
			)
		);

    }

    /**
     * Set the tags whitelist
     *
     * @param array $tags
     */
    public function setAllowTags($tags){
        $this->setTagnameWhitelist($tags);
    }

    /**
     * Set the attributes whitelist
     *
     * @param array $atts
     */
    public function setAllowAtts($atts){
        $this->setAttributeWhitelist($atts);
    }

    /**
     * Return the tags whitelist as array
     *
     * @return array $allowTags
     */
    public function getAllowTags(){
        return $this->getTagnameWhitelist();
    }
    
    /**
     * Return the attributes whitelist as array
     *
     * @return array $allowAtts
     */
    public function getAllowAtts(){
        return $this->getAttributeWhitelist();
    }

    /**
     * Get a value from the $_POST array
     *
     * @param string $key
     * @param int $strlen
     * @param mixed $retval
     * @return mixed $value
     */
    public function getPostValue($key, $strlen='', $retval=false){
        if($strlen == '') $strlen = $this->strlen;

        if(array_key_exists($key, $this->post)){
            if(!is_array($this->post[$key])){
                $retval = $this->process($this->post[$key]);
                $retval = substr($retval, 0, $strlen);
            }else{
                $retval = $this->post[$key];
                foreach($retval as $k=>$v){
                    $v = $this->process($v);
                    $retval[$k] = substr($v, 0, $strlen);
                };
                $retval = $this->post[$key];
            };
        };
        return $retval;
    }

    /**
     * Get a value from the $_FILES array
     *
     * @param string $key
     * @param int $strlen
     * @param mixed $retval
     * @return mixed $value
     */
    public function getFilesValue($key, $strlen='', $retval=false){
        if($strlen == '') $strlen = $this->strlen;
        if(array_key_exists($key, $this->files)){
            if(!is_array($this->files[$key])) $retval = substr($this->files[$key], 0, $strlen);
            else{
                foreach($this->files[$key] as $k=>$v){
                    $this->files[$key][$k] = substr($v, 0, $strlen);
                };
                $retval = $this->files[$key];
            };
        };
        return $retval;
    }

    /**
     * Get a value from the $_GET array
     *
     * @param string $key
     * @param int $strlen
     * @param mixed $retval
     * @return mixed $value
     */
    public function getGetValue($key, $strlen='', $retval=false){
        if($strlen == '') $strlen = $this->strlen;
        if(array_key_exists($key, $this->get)){
            if(!is_array($this->get[$key])) $retval = substr($this->get[$key], 0, $strlen);
            else{
                foreach($this->get[$key] as $k=>$v){
                    $this->get[$key][$k] = substr($v, 0, $strlen);
                };
                $retval = $this->get[$key];
            };
        };
        return $retval;
    }
}


